const { Sequelize } = require('sequelize');
require('dotenv').config();

const sequelize = new Sequelize(
  process.env.DB_NAME || 'metaverse_pets',
  process.env.DB_USER || 'root',
  process.env.DB_PASSWORD || '',
  {
    host: process.env.DB_HOST || 'localhost',
    dialect: 'mysql',
    logging: false,
    timezone: '+08:00', // 设置时区为东八区
    define: {
      timestamps: true,
      underscored: false,
      createdAt: 'createdAt',
      updatedAt: 'updatedAt'
    },
    pool: {
      max: 5,
      min: 0,
      acquire: 30000,
      idle: 10000
    },
    sync: {
      force: false, // 设置为 false 以保持数据库数据
      alter: false  // 禁用自动更新表结构，避免索引问题
    }
  }
);

// 测试数据库连接
const testConnection = async () => {
  try {
    await sequelize.authenticate();
    console.log('已成功建立数据库连接。');
  } catch (error) {
    console.error('无法连接到数据库：', error);
    process.exit(1);
  }
};

testConnection();

module.exports = sequelize;